介紹
接下來我們使用 puppeteer的分析功能把抓到的 資料透過 evaluate 取到 outerHTML,因為也不想一直在網路上測試所以會把資料存成靜態檔案
var data = await page.content()
fs.writeFile(`page${i}.html`, data, (err) => console.log(err));
Puppeteer就使用 local file的功能搭配 evaluate 使用,local mode 要記得使用 schema為 file:///
import * as puppeteer from 'puppeteer'
(async function go() {
const browser = await puppeteer.launch({
headless: false,
args: ['--start-maximized'],
//userDataDir: "./userData/"
});
const page = await browser.newPage();
await page.setUserAgent(`Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36`)
//console.log('test use agent')
// await watchDog;
let currentScreen = await page.evaluate(() => {
return {
width: window.screen.availWidth,
height: window.screen.availHeight,
};
});
await page.setViewport(currentScreen);
try {
await page.goto('file:///Users/user/Sites/case/puppeteerProject/getOpenData/page1.html')
let handle = await page.$$(".line");
handle.forEach(async (v) => {
let temp = await page.evaluate((data: HTMLElement) => {
// console.log(data)
return data.outerHTML;
}, v)
console.log(temp)
})
} catch (err) {
console.log('err')
}
})()